<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include ("../../include/image_slider.php");
include ("../../include/function.php");
include ("../../include/dbFunctions.php");

if (isset($_SESSION['school_id'])) {
    $no_session = $_POST['no_session'];

    $date_array = array();
    $time_array = array();

    for ($i = 0; $i < $no_session; $i++) {
        $date_array[] = $_POST['date' . $i . ''];
        $time_array[] = $_POST['time' . $i . ''];
        $end_time_array[] = $_POST['end_time' . $i . ''];
    }

    $program_id = $_POST['program_id'];
    $no_class = $_POST['no_class'];
    $class_level = $_POST['class_level'];


    $msg = '';
    $duration = array();
    $true = 0;

    for ($i = 0; $i < count($time_array); $i++) {
        $new_time = explode(":", $time_array[$i]);
        $new_time = $new_time[0] * 60 + $new_time[1];

        $new_end_time = explode(":", $end_time_array[$i]);
        $new_end_time = $new_end_time[0] * 60 + $new_end_time[1];
        $duration[] = $new_end_time - $new_time;
        if ($new_end_time < $new_time) {
            $true = $true + 1;
            $num = $i+1;
            $msg.= "End time is earlier than start time at session number $num! <br>";
        }
    }

    $total = 0;
    $true1 = 0;

    $program_duration = "SELECT * FROM program WHERE program_id = $program_id";
    $program_duration = executeSelectQuery($program_duration);

    if ($true == 0) {
        for ($i = 0; $i < count($duration); $i++) {
            $total = $total + $duration[$i];
        }
        $total_hour = $total / 60;
        $hour = floor($total_hour);
        $minute = $total % 60;

        $require = explode(":", $program_duration[0]['program_lesson_duration']);
        $require = $require[0] * 60 + $require[1];
        $original = $program_duration[0]['program_lesson_duration'];
        if ($require > $total) {
            $true1 = $true1 + 1;
            $msg.= "Your sessions total duration is " . $hour . ":" . $minute . ". However, the total duration of this course is " . $original . ".<br>";
           
        }
    }
  
    
    $array = array();
    $new_array= array();
    $error = 0;
    $error1 = 0;
    for ($i = 0; $i < count($date_array); $i++){
        $array[] = $date_array[$i].'-'.$time_array[$i];
    }
    for ($t = 0; $t < count($array); $t++){
        for($j = 0; $j < count($array); $j++){
            if ($t != $j){
                if ($array[$t] == $array[$j]){
                    $error = $error + 1;
                    
                }
            }
        }
    }
    
    if ($error!=0){
        $msg.="There are sessions that have the same day and start time.<br>";
    }
    
    $munite1_array = array();
    $munite2_array = array();
    for ($i = 0; $i < count($time_array); $i++) {
        $minute1 = explode(":", $time_array[$i]);
        $minute1 = $minute1[0] * 60 + $minute1[1];
        $munite1_array[] = $minute1;
        
        $minute2 = explode(":", $end_time_array[$i]);
        $minute2 = $minute2[0] * 60 + $minute2[1];
        $munite2_array[] = $minute2;
    }
    
    for ($i = 0; $i < count($date_array); $i++){
        $new_array[] = $date_array[$i].'-'.$munite1_array[$i].'-'.$munite2_array[$i];
    }
    for ($i = 0; $i < count($new_array); $i++){
        for($t = 0; $t < count($new_array); $t++){
            if ($i != $t){
                $com1 = explode("-", $new_array[$i]);
                $com2 = explode("-", $new_array[$t]);
                if ($com1[0] == $com2[0] &&  (($com2[1] > $com1[1] && $com2[1] < $com1[2])|| ($com2[2] > $com1[1] && $com2[2] < $com1[2]  ) )    ){
                    $error1 = $error1 + 1;
                    
                }
            }
        }
    }
    if ($error1 != 0){
        $msg.= "There are overlap sessions<br>";
    }
    
    
    if ($true != 0) {
        $msg.= "Please go back and modify your sessions.<br> <input type='button' value='' class='form-back' onclick='history.go(-2)'/>";
    }
    if ($true == 0 && $true1 != 0) {
         $msg.= "Please go back and modify your sessions.<br> <input type='button' value='' class='form-back' onclick='history.go(-2)'/>";
    }
    if ($true == 0 && $true1 == 0 && $error != 0){
        $msg.= "Please go back and modify your sessions.<br> <input type='button' value='' class='form-back' onclick='history.go(-2)'/>";
    }
    if ($true == 0 && $true1 == 0 && $error == 0 && $error1 != 0){
        $msg.= "Please go back and modify your sessions.<br> <input type='button' value='' class='form-back' onclick='history.go(-2)'/>";
    }
    
}
?>


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Junior Achievement Singapore</title>

        <style type="text/css">
            @import "../../media/css/jquery-ui-1.8.13.custom.css";
            /* css for formToWizard Plugin */

            #main { width:100%; margin: 0px auto; border:solid 1px #b2b3b5; -moz-border-radius:10px; padding:20px; background-color:#f6f6f6;}

            fieldset { border:none; width:320px;}
            legend { font-size:12px; margin:0px; padding:10px 0px; color:#666666; font-weight:bold;}
            label { display:block; margin:15px 0 5px; font-size:12px}
            .prev { float:left; background: url(../../media/images/forms/form_back.gif) no-repeat; height:30px; width: 80px; cursor: pointer; margin: 5px 0px 0px 0px;}
            .next { float:right; background: url(../../media/images/forms/form_next.gif) no-repeat; height:30px; width: 80px; cursor: pointer; margin: 5px 0px 0px 0px;}
            #steps { list-style:none; width:100%; overflow:hidden; margin:0px; padding:0px;}
            #steps li {font-size:24px; float:left; padding:10px; color:#b0b1b3;}
            #steps li span {font-size:12px; display:block;}
            #steps li.current { color:#000;}
            #makeWizard { background-color:#b0232a; color:#fff; padding:5px 10px; text-decoration:none; font-size:18px;}
            #makeWizard:hover { background-color:#000;}

        </style>

        <link href="../../media/css/styles.css" rel="stylesheet" type="text/css" />
        <link href="../../media/css/jquery-ui-1.8.13.custom.css" rel="stylesheet" type="text/css" />

        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" src="../../media/js/jqueryUI/jquery-ui-1.8.13.custom.js"></script>
        <script type="text/javascript" src="../../media/js/jquery/image_slider.js"></script>
        <!-- Custom jquery scripts -->

        <script type="text/javascript" language="javascript" src="../../media/js/jquery/jquery.validate.min.js"></script>
        <script type="text/javascript" src="../../media/js/jquery/validator_rules.js"></script>
        <script type="text/javascript">
            var no = "<?= $no_class ?>";
            var i=0;

            $(document).ready(function(){
                $("#eventRegistration").validate();
                for(i=0; i<no; i++){
                    var final_name = "no_student"+i;
                
                    $("#"+final_name+"").rules("add", {number:true});


                }
                   
            })
        </script>
    </head>
    <body>
        <script>
            var timeout    = 500;
            var closetimer = 0;
            var ddmenuitem = 0;

            function jsddm_open()
            {  jsddm_canceltimer();
                jsddm_close();
                ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

            function jsddm_close()
            {  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

            function jsddm_timer()
            {  closetimer = window.setTimeout(jsddm_close, timeout);}

            function jsddm_canceltimer()
            {  if(closetimer)
                {  window.clearTimeout(closetimer);
                    closetimer = null;}}

            $(document).ready(function()
            {  $('#navigation > li').bind('mouseover', jsddm_open)
                $('#navigation > li').bind('mouseout',  jsddm_timer)});

            document.onclick = jsddm_close;

        </script>
        <!-- End choose date and time script -->
        <div id="container"><!--CONTAINER STARTS-->
            <div id="navigation">
                <li><a href="../../include/home/homepage.php">HOME</a></li>
                <li><a href="../../include/home/contact.php">CONTACT</a></li>
                <li><a href="http://ja.org.sg/">ABOUT JA SINGAPORE</a></li>
                <li><a href="http://jascheduling.lefora.com/">FEEDBACK</a></li>
                <li><a href="../../guide/guide_menu.php">HELP</a></li>

            </div>

            <div id="title"><!--TITLE STARTS-->
            </div><!--TITLE ENDS-->

            <div id="featured"><!--FEATURED AREA STARTS-->

                <div class="featured-image"><!--FEATURED IMAGE STARTS-->
                    <img alt="Featured Image" src="../../media/images/featured_image.png"/>

                </div><!--FEATURED IMAGE ENDS-->

                <div class="featured-text"><!--FEATURED TEXT STARTS-->

                    <h2>Welcome to JA Singapore</h2>
                    <p>Junior Achievement programs help prepare young people for the real world by showing them how to generate wealth and effectively manage it, how to create jobs which make their communities more robust, and how to apply entrepreneurial thinking to the workplace. <a href="http://www.jaworldwide.org/">Read More... </a></p>

                </div><!--FEATURED TEXT ENDS-->

            </div><!--FEATURED AREA ENDS-->

            <?php
            custom_login();
            ?>


            <div id="left-content"><!--RIGHT CONTENT STARTS-->

                <div class="left-top"><!--RIGHT CONTENT BOX TOP-->
                </div><!--RIGHT CONTENT BOX TOP ENDS-->

                <div class="left-middle"><!--RIGHT CONTENT BOX MIDDLE-->

                    <?php
                    if (!isset($_SESSION['school_id'])) {
                        echo "<br/><br/><br/><br/><br/><center><h3>You don't have permission to view this page<h3><br/>
 Please login to access the page.</center><br/><br/><br/><br/><br/><br/><br/><br/>";
                    } else {
                        if ($true!= 0 || $true1 != 0 ||$error != 0 || $error1 != 0) {
                            echo "<br><br><br>".$msg ;
                        } else {
                            ?>
                            <h3>Request Course</h3> <!-- Request event by fill in required fields below-->

                            <table>
                                <tr>
                                    <td><center>Step 1</center></td>
                                    <td><center>Step 2</center></td>
                                    <td><b><center>Step 3</center></b></td>
                                </tr>
                                <tr>
                                    <td>Select Program -- </td>
                                    <td> Session date and time -- </td>
                                    <td><b>Class Details</b></td>
                                </tr>
                                <?php ?>

                            </table>
                            <form id="eventRegistration" method ="POST" action="doRequestEvent.php"  name="frmData" enctype="multipart/form-data" >

                                <?php
                                for ($i = 0; $i < $no_class; $i++) {
                                    ?>
                                <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
                                    <tr>
                                        <th valign="top"># <?php echo $i + 1 ?> Class Name<em>*</em></th>
                                        <td><input type="text" class="inp-form"  name="class_name<?php echo $i ?>" value="" class="required"/></td>
                                    </tr>
                                    <tr>
                                        <th valign="top">Number of student<em>*</em></th>
                                        <td><input type="text" class="inp-form" id ="no_student<?php echo $i ?>" name="no_student<?php echo $i ?>" value="" class="num required"/><br/></td>
                                    </tr>
                                    <tr>
                                        <th valign="top">Upload student list<em>*</em></th>
                                        <td><input type="file" id="file" class="inp-form" name="file<?php echo $i ?>"  class="required"/></td>                                    
                                    </tr>
                                </table>
                                            
                                            <br/><br/>
                                                    <?php
                                                }
                                                ?>
                                                <br/><br/>
                                                        Please click Submit button below if you want to send this course request!
                                                        <br/>
                                                            <table border="0">
                                                                <tr>
                                                                    <td> <input type='button' value="" class="form-back" onclick='history.go(-1)'/></td>
                                                                    <td><input type="submit"  value="" class ="form-submit" /></td>
                                                                </tr>

                                                            </table>
                                                            <input type="hidden" name="program_id" value="<?php echo $program_id ?>"/>
                                                                <input type="hidden" name="no_class" value="<?php echo $no_class ?>"/>
                                                                    <input type="hidden" name="class_level" value="<?php echo $class_level ?>"/>
                                                                        <input type="hidden" name="no_session" value="<?php echo $no_session ?>"/>

                                                                            <?php
                                                                            for ($i = 0; $i < count($date_array); $i++) {
                                                                                ?>
                                                                                <input type="hidden" name="date_array[]" value="<?php echo $date_array[$i] ?>"/>
                                                                                    <input type="hidden" name="time_array[]" value="<?php echo $time_array[$i] ?>"/>
                                                                                        <input type="hidden" name="end_time_array[]" value="<?php echo $end_time_array[$i] ?>"/>
                                                                                            <?php
                                                                                        }
                                                                                        ?>
                                                                                        </form>
                                                                                        <?php
                                                                                    }
                                                                                }
                                                                                ?>
                                                                                </div><!--RIGHT CONTENT BOX MIDDLE ENDS-->
                                                                                <div class="left-bottom"><!--RIGHT CONTENT BOX BOTTOM-->
                                                                                </div><!--RIGHT CONTENT BOX BOTTOM ENDS-->
                                                                                </div><!--RIGHT CONTENT ENDS-->
                                                                                </div><!--CONTAINER ENDS-->
                                                                                <div id="footer"><!--FOOTER STARTS-->
                                                                                    <p>Copyright &copy; Junior Achievement Singapore | All Rights Reserved</p>
                                                                                </div><!--FOOTER ENDS-->
                                                                                </body>
                                                                                </html>
